home *** CD-ROM | disk | FTP | other *** search
Wrap
#!/usr/bin/perl # Blosxom # Author: Rael Dornfest <rael@oreilly.com> # Version: 1.2 # Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ # --- Configurable variables ----- # What's this blog's title? my $blog_title = "My Blosxom"; # What's this blog's description (for outgoing RSS feed)? my $blog_description = "Yet another Blosxom blog."; # What's this blog's primary language (for outgoing RSS feed)? my $blog_language = "en"; # Where are this blog's entries kept? my $datadir = "/Library/WebServer/Documents/blosxom"; # What's my preferred base URL for this blog (leave blank for automatic)? my $url = ""; # Should I stick only to the datadir for items or travel down the # directory hierarchy looking for items? If so, to what depth? # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down my $depth = 0; # How many entries should I show on the home page? my $num_entries = 40; # What file extension signifies a blosxom entry? my $file_extension = "txt"; # What is the default flavour? my $default_flavour = "html"; # --- Static Rendering ----- # Where are this blog's static files to be created? my $static_dir = "/Library/WebServer/Documents/blog"; # What's my administrative password (you must set this for static rendering)? my $static_password = ""; # What flavours should I generate statically? my @static_flavours = qw/html rss/; # Should I statically generate individual entries? # 0 = no, 1 = yes my $static_entries = 0; # -------------------------------- use strict; use FileHandle; use File::Find; use File::stat; use Time::localtime; use CGI qw/:standard :netscape/; my %month2num = (nil=>'00', Jan=>'01', Feb=>'02', Mar=>'03', Apr=>'04', May=>'05', Jun=>'06', Jul=>'07', Aug=>'08', Sep=>'09', Oct=>'10', Nov=>'11', Dec=>'12'); my @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num; # Use the stated preferred URL or figure it out automatically $url ||= url(); $url =~ s/^included:/http:/; # Fix for Server Side Includes (SSI) $url =~ s!/$!!; # Fix depth to take into account datadir's path $depth and $depth += ($datadir =~ tr[/][]) - 1; # Global variable to be used in head/foot.{flavour} templates my $path_info = ''; my $fh = new FileHandle; # Bring in the templates my %template = (); while (<DATA>) { last if /^(__END__)?$/; my($ct, $comp, $txt) = /^(\S+)\s(\S+)\s(.*)$/; $txt =~ s/\\n/\n/mg; $template{$ct}{$comp} = $txt; } my ($d, %files, %indexes, %entries); find( sub { my $curr_depth = $File::Find::dir =~ tr[/][]; return if $depth and $curr_depth > $depth; $File::Find::name =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$! and $2 ne 'index' and $2 !~ /^\./ and $files{$File::Find::name} = stat($File::Find::name)->mtime and ( param('-all') or !-T "$static_dir/$1/index." . $static_flavours[0] or stat("$static_dir/$1/index." . $static_flavours[0])->mtime < stat($File::Find::name)->mtime ) and $indexes{$1} = 1 and $d = join('/', (nice_date(stat($File::Find::name)->mtime))[5,2,3]) and $indexes{$d} = $d and $static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$file_extension" } = 1; }, $datadir ); # Static if (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) { param('-quiet') or print "Blosxom is generating static index pages...\n"; # Home Page and Directory Indexes my %done; foreach my $path ( sort keys %indexes) { my $p = ''; foreach ( ('', split /\//, $path) ) { $p .= "/$_"; $p =~ s!^/!!; $done{$p}++ and next; (-d "$static_dir/$p" or $p =~ /\.$file_extension$/) or mkdir "$static_dir/$p", 0755; foreach my $flavour ( @static_flavours ) { chomp(my $content_type = $fh->open("< $datadir/$p/content_type.$flavour") || $fh->open("< $datadir/content_type.$flavour") ? <$fh> : ($template{$flavour}{'content_type'} || 'text/plain')); my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index"; param('-quiet') or print "$fn.$flavour\n"; my $fh_w = new FileHandle "> $static_dir/$fn.$flavour" or die "Couldn't open $static_dir/$p for writing: $!"; print $fh_w $indexes{$path} == 1 ? &generate('static', $p, '', $flavour, $content_type) : &generate('static', '', $p, $flavour, $content_type); $fh_w->close; } } } } # Dynamic else { # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day my @path_info = split m{/}, path_info(); shift @path_info; while ($path_info[0] and $path_info[0] =~ /^[a-zA-Z].*$/ and $path_info[0] !~ /(.*)\.(.*)/) { $path_info .= '/' . shift @path_info; } # An icky bit of backward-compatibility with Blosxom < 0+5i; remove the # to enable #path_info() =~ m!/xml/?$! and $flavour = 'rss'; # Flavour specified by ?flav={flav} or index.{flav} my $flavour; if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) { $flavour = $2; $1 ne 'index' and $path_info .= "/$1.$2"; pop @path_info; } else { $flavour = param('flav') || $default_flavour; } $path_info =~ s!^/!!; # Date fiddling my($path_info_yr,$path_info_mo,$path_info_da) = @path_info; my $path_info_mo_num = $path_info_mo ? ( $path_info_mo =~ /\d{2}/ ? $path_info_mo : ($month2num{ucfirst(lc $path_info_mo)} || undef) ) : undef; chomp(my $content_type = $fh->open("< $datadir/$path_info/content_type.$flavour") || $fh->open("< $datadir/content_type.$flavour") ? <$fh> : ($template{$flavour}{'content_type'} || 'text/plain')); print header($content_type); print generate('dynamic', $path_info, "$path_info_yr/$path_info_mo_num/$path_info_da", $flavour, $content_type); } # Generate sub generate { my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_; my $return = ''; # Header my $head = join '', ($fh->open("< $datadir/$currentdir/head.$flavour") || $fh->open("< $datadir/head.$flavour") ? <$fh> : ($template{$flavour}{'head'} || $template{'error'}{'head'})); $head =~ s/(\$\w+)/$1 . "||''"/gee; $return .= $head; # Stories my $curdate = ''; my $ne = $num_entries; my %f; if ( $currentdir =~ /(.+?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) { $currentdir = "$1$2.$file_extension"; $files{"$datadir/$1$2.$file_extension"} and %f = ( "$datadir/$1$2.$file_extension" => $files{"$datadir/$1$2.$file_extension"} ); } else { $currentdir =~ s!/index\..+$!!; %f = %files; } foreach my $path_file ( sort { $files{$b} <=> $files{$a} } keys %f ) { last if $ne <= 0 && $date !~ /\d/; my($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!; # Only stories in the right hierarchy $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next; # Prepend a slash for use in templates only if a path exists $path &&= "/$path"; # Date fiddling for by-{year,month,day} archive views my ($dw,$mo,$mo_num,$da,$ti,$yr) = nice_date(stat("$path_file")->mtime); my ($hr,$min) = split /:/, $ti; # Only stories from the right date my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date; next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr; next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num]; next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da; # Date display if ( $curdate ne "$da/$mo/$yr" ) { my $date = join '', ($fh->open("< $datadir/$path/date.$flavour") || $fh->open("< $datadir/date.$flavour") ? <$fh> : ($template{$flavour}{'date'} || $template{'html'}{'date'})); $date =~ s/(\$\w+)/$1 . "||''"/gee; $curdate = "$da/$mo/$yr"; $return .= $date; } if (-T "$path_file" && $fh->open("< $path_file")) { chomp(my $title = <$fh>); chomp(my $body = join '', <$fh>); if ($content_type =~ m{\Wxml$}) { # Escape <, >, and &, and to produce valid RSS my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); my $escape_re = join '|' => keys %escape; $title =~ s/($escape_re)/$escape{$1}/g; $body =~ s/($escape_re)/$escape{$1}/g; } $fh->close; my $story = join '', ($fh->open("< $datadir/$path/story.$flavour") || $fh->open("< $datadir/story.$flavour") ? <$fh> : ($template{$flavour}{'story'} || $template{'error'}{'story'})); $story =~ s/(\$\w+)/$1 . "||''"/gee; $return .= $story; $fh->close; $ne--; } } # Footer my $foot = join '', ($fh->open("< $datadir/$currentdir/foot.$flavour") || $fh->open("< $datadir/foot.$flavour") ? <$fh> : ($template{$flavour}{'foot'} || $template{'error'}{'foot'})); $foot =~ s/(\$\w+)/$1 . "||''"/gee; $return .= $foot; $return; } sub nice_date { my($unixtime) = @_; my $c_time = ctime($unixtime); my($dw,$mo,$da,$ti,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}:\d{2}):\d{2} +(\d{4})$/ ); $da = sprintf("%02d", $da); my $mo_num = $month2num{$mo}; return ($dw,$mo,$mo_num,$da,$ti,$yr); } # Default HTML and RSS template bits __DATA__ html content_type text/html html head <html><head><link rel="alternate" type="type="application/rss+xml" title="RSS" href="$url/?flav=rss" /><title>$blog_title $path_info_da $path_info_mo $path_info_yr</title></head><body><center><font size="+3">$blog_title</font><br />$path_info_da $path_info_mo $path_info_yr</center><p /> html story <p><a name="$fn"><b>$title</b></a><br />$body<br /><br />posted at: $ti | path: <a href="$url$path">$path</a> | <a href="$url/$yr/$mo_num/$da#$fn">permanent link to this entry</a></p>\n html date <h3>$dw, $da $mo $yr</h3>\n html foot <p /><center><a href="http://www.raelity.org/apps/blosxom/"><img src="http://www.raelity.org/images/pb_blosxom.gif" border="0" /></body></html> rss content_type text/xml rss head <?xml version="1.0"?>\n<!-- name="generator" content="bloxsom/1.1" -->\n<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">\n\n<rss version="0.91">\n <channel>\n <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>\n <link>$url</link>\n <description>$blog_description</description>\n <language>$blog_language</language>\n rss story <item>\n <title>$title</title>\n <link>$url/$yr/$mo_num/$da#$fn</link>\n <description>$body</description>\n </item>\n rss date \n rss foot </channel>\n</rss> error head Error: I'm afraid this is the first I've heard of a "$flavour" flavoured Blosxom. Try dropping the "/+$flavour" bit from the end of the URL.\n\n html content_type text/html __END__